package com.ruoyi.common.datascope.aspect;


import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.datascope.annotation.ServiceScope;
import com.ruoyi.common.datascope.exception.ServiceSelectorException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;

@Aspect
@Component
public class ServiceSelectorAspect {
    @Around("@annotation(com.ruoyi.common.datascope.annotation.ServiceScope)")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Signature signature = point.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        ServiceScope annotation = method.getAnnotation(ServiceScope.class);
        if (annotation == null) {
            throw new ServiceSelectorException("Annotation cannot be empty");
        }

        String controllerField = annotation.field();
        String value = annotation.value();
        if (StringUtils.isBlank(controllerField) && StringUtils.isBlank(value)) {
            throw new ServiceSelectorException("Not Null");
        }

        if (StringUtils.isBlank(controllerField)) {
            controllerField = value;
        }

        Object controller = point.getTarget();
        Class<?> controllerClass = controller.getClass();
        Field[] fields = controllerClass.getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            String name = field.getName();
            if (!StringUtils.equals(name, controllerField)) {
                continue;
            }

            Class<?> type = field.getType();
            if (!type.isInterface()) {
                throw new ServiceSelectorException("This class is not an interface");
            }
/*
            Map<String, ?> beanImpls = SpringUtils.getBeanImpls(type);
            for (String key : beanImpls.keySet()) {
                Object bean = beanImpls.get(key);

                if (isPlatform() && bean instanceof IPlatformService) {
                    field.set(controller, bean);
                    return point.proceed();
                }

                if (!isPlatform() && bean instanceof IBusinessService) {
                    field.set(controller, bean);
                    return point.proceed();
                }

                // TODO: 2020/12/24 目前之分平台和运营端，因为有的用户只注册了，没有分配企业
               /*if (isBusiness() && bean instanceof IBusinessService) {
                    field.set(controller, bean);
                    return point.proceed();
                }

                if (isOperator() && bean instanceof IOperatorService) {
                    field.set(controller, bean);
                    return point.proceed();
                }*/
           // }
        }

        throw new ServiceSelectorException("Corresponding implementation not found");
    }

    /**
     * 是否为平台端
     *
     * @return 结果
     */
  /*
    public static boolean isPlatform() {
        return getBusId() != null && PLATFORM_ID == getBusId();
    }
*/
    /**
     * 是否为商户
     * 企业类型 1：商户；2：运营商
     *
     * @return
     */
    /*
    public static boolean isBusiness() {
        Long enterpriseType = Convert.toLong(ServletUtils.getRequest().getHeader(CacheConstants.ENTERPRISE_TYPE));
        return enterpriseType != null && BUSINESS == enterpriseType;
    }
*/
    /**
     * 是否为商户
     * 企业类型 1：商户；2：运营商
     *
     * @return
     */
    /*
    public static boolean isOperator() {
        Long enterpriseType = Convert.toLong(ServletUtils.getRequest().getHeader(CacheConstants.ENTERPRISE_TYPE));
        return enterpriseType != null && OPERATOR == enterpriseType;
    }
*/
    /**
     * 获取业务系统id
     */
    /*
    public static Long getBusId() {
        return Convert.toLong(ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_BUS_ID));
    }
*/
}
